import java.util.*;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    public int[] levelOrder(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root != null) queue.add(root);
        while (!queue.isEmpty()) {
            root = queue.poll();
            list.add(root.val);
            if (root.left != null) {
                queue.add(root.left);
            }
            if (root.right != null) {
                queue.add(root.right);
            }
        }
        int[] ints = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ints[i] = list.get(i);
        }
        return ints;
    }

    public static void main(String[] args) {
        TreeNode t17 = new TreeNode(7, null, null);
        TreeNode t15 = new TreeNode(15, null, null);
        TreeNode t20 = new TreeNode(20, t15, t17);
        TreeNode t9 = new TreeNode(9, null, null);
        TreeNode t3 = new TreeNode(3, t9, t20);
        int[] res = new Solution().levelOrder(t3);
        System.out.println(Arrays.toString(res));
    }
}

